package jbeagle.ui;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import jbeagle.core.FitnessChangeEvent;
import jbeagle.core.FitnessChangeListener;
import jbeagle.core.Population;

public class TrackingGUI implements FitnessChangeListener {

	protected XYSeries avgFitness, bestFitness;
	
	protected Population currentPop;
	
	public void init() {
		avgFitness = new XYSeries("Average Fitness", false, false);
		bestFitness = new XYSeries("Best Fitness", false, false);
		
		XYSeriesCollection xyDataset = new XYSeriesCollection(avgFitness);
		xyDataset.addSeries(bestFitness);
		
		JFreeChart chart = ChartFactory.createXYLineChart("Fitness", "Generation", "Fitness", xyDataset, PlotOrientation.VERTICAL, true, false, false);
		
		ChartPanel chartArea = new ChartPanel(chart, true);
		
		JFrame f = new JFrame("GA Tracker");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(chartArea, "Center");
        f.pack();
        f.setVisible(true);
	}
	
	@Override
	public void fitnessChanged(FitnessChangeEvent e) {
		currentPop = e.getPopulation();
		avgFitness.add(e.getCurrentGeneration(), currentPop.getAvgFitness());
		bestFitness.add(e.getCurrentGeneration(), currentPop.getFittest().getFitness());		
	}

}
